﻿using System;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraEditors;
using EduFormManager.Models;
using EduFormManager.Models.Repo;
using EduFormManager.Properties;

namespace EduFormManager.Forms.UserControls
{
    public partial class XtraDictionaryMunitControl : XtraBaseControl, ISupportDocumentActions
    {
        public XtraDictionaryMunitControl(WindowsUIView view)
            : base(view)
        {
            InitializeComponent();

            this.labelSavedStatus.Text = String.Empty;
            this.pictureBoxSavedStatusIcon.Visible = false;
        }

        private void listBoxMunicipality_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBoxControl listBox = (ListBoxControl)sender;
            municipalityBindingSource.Position = listBox.SelectedIndex;

            Municipality munit = (Municipality)municipalityBindingSource.Current;

            listBoxMunitEdus.Items.Clear();
            listBoxMunitEdus.Items.AddRange(munit.EduOrgs.ToArray<EduOrg>());

            this.munitEditControl.Municipality = munit;

            this.labelSavedStatus.Text = string.Empty;
            this.pictureBoxSavedStatusIcon.Visible = false;
        }
        public void New()
        {
            New(null);
        }
        protected override void New(Document doc)
        {
            municipalityBindingSource.AddNew();
            XtraDictionaryEditMunitForm editForm = new XtraDictionaryEditMunitForm(this, municipalityBindingSource.Current as Municipality);

            editForm.Show(this.ParentForm);
            this.labelSavedStatus.Text = "В справочнике Образовательных Организаций муниципалитет появится после добавления к нему организации";
            this.pictureBoxSavedStatusIcon.Visible = true;
        }
        public bool CanNew()
        {
            return CanNew(null);
        }
        public void CancelNew()
        {
            Municipality current = municipalityBindingSource.Current as Municipality;
            if (current.Id == 0)
                municipalityBindingSource.Remove(current);
        }
        protected override bool CanNew(Document doc)
        {
            return true;
        }
        public void Save()
        {
            Save(null);
        }
        protected override void Save(Document doc)
        {
            if (this.CanSave(doc))
            {
                this.munitEditControl.Save();
            }
        }
        public bool CanSave()
        {
            return CanSave(null);
        }
        protected override bool CanSave(Document doc)
        {
            return this.munitEditControl.CanSave();
        }
        public void Remove()
        {
            Remove(null);
        }
        protected override void Remove(Document doc)
        {
            Municipality munit = (Municipality)municipalityBindingSource.Current;

            if (munit.EduOrgs.Count > 0)
            {
                MessageBox.Show(this,
                    String.Format("нельзя удалить муниципалитет \"{0}\", пока ему принадлежат следующие организации: {1}.", munit, munit.EduOrgs),
                    "Уведомление",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return;
            }
            RepoHelper.Repo.Delete(munit);

            municipalityBindingSource.RemoveCurrent();
            municipalityBindingSource.EndEdit();
            
            base.UpdateData();
        }
        public bool CanRemove()
        {
            return CanRemove(null);
        }
        protected override bool CanRemove(Document doc)
        {
            return municipalityBindingSource.Current != null;
        }

        public void OnQueryDocumentActions(IDocumentActionsArgs args)
        {
            args.DocumentActions.Add(new DocumentAction(New) { Caption = "Новый", Image = Resources.glyphicons_432_plus });
            args.DocumentActions.Add(new DocumentAction(Save) { Caption = "Сохранить", Image = Resources.glyphicons_206_ok_2 });
            args.DocumentActions.Add(new DocumentAction(CanRemove, Remove) { Caption = "Удалить", Image = Resources.glyphicons_207_remove_2 });
        }
    }
}
